package com.jt.www.domain.bo;

import com.alibaba.fastjson.JSON;
import com.jt.www.domain.enums.common.BusinessTypeCodeEnum;
import com.jt.www.domain.po.OfflineOrder;
import com.jt.www.domain.po.OfflinePolicy;
import com.jt.www.domain.vo.FileVO;
import com.jt.www.domain.vo.RiskDutyLimitVO;
import com.jt.www.util.MyStringUtils;
import io.swagger.annotations.ApiModelProperty;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;

/**
 * 线下投保定单业务对象
 * Created by shaoyn on 2020/05/22.
 */
public class OfflineOrderBO {

    public static final Logger log = LoggerFactory.getLogger(OfflineOrderBO.class);

    @ApiModelProperty(value = "线下录入定单")
    private OfflineOrder offlineOrder;

    @ApiModelProperty(value = "线下录入保单集合")
    private List<OfflinePolicy> offlinePolicies;

    @ApiModelProperty(value = "线下录入定单")
    private List<RiskDutyLimitVO> riskDutyLimits;

    @ApiModelProperty(value = "保单导入的文件")
    private List<FileVO> policyFiles;

    @ApiModelProperty(value = "附件文件")
    private List<FileVO> attachedFiles;

    @ApiModelProperty(value = "佐证文件")
    private List<FileVO> evidenceFiles;


    public OfflineOrderBO() {
    }

    public OfflineOrderBO(OfflineOrder offlineOrder, List<OfflinePolicy> offlinePolicies) {
        this.offlineOrder = offlineOrder;
        this.offlinePolicies = offlinePolicies;
    }

    public List<RiskDutyLimitVO> getRiskDutyLimits() {
        if (null == offlineOrder || StringUtils.isBlank(offlineOrder.getRiskDutyLimit())) {
            return riskDutyLimits;
        }
        try {
            riskDutyLimits = JSON.parseArray(offlineOrder.getRiskDutyLimit(), RiskDutyLimitVO.class);
        } catch (Exception e) {
            log.error("ID为{}的线下投保单，险种信息json格式不正确!", offlineOrder.getId());
        }
        return riskDutyLimits;
    }

    public void setRiskDutyLimits(List<RiskDutyLimitVO> riskDutyLimits) {
        this.riskDutyLimits = riskDutyLimits;
    }

    public List<FileVO> getPolicyFiles() {
        if (null == offlineOrder || StringUtils.isBlank(offlineOrder.getPolicyFile())) {
            return policyFiles;
        }
        try {
            List<FileVO> fileVOS = JSON.parseArray(offlineOrder.getPolicyFile(), FileVO.class);
            for (FileVO fileVO : fileVOS) {
                fileVO.setFileUrl(MyStringUtils.createFileUrl("edu-svc", BusinessTypeCodeEnum.F001.getCode(), String.valueOf(offlineOrder.getId()), "0", fileVO.getFileId()));
            }
            return fileVOS;
        } catch (Exception e) {
            log.error("ID为{}的线下投保单，保单导入的文件Json格式不正确!", offlineOrder.getId());
        }
        return policyFiles;
    }

    public void setPolicyFiles(List<FileVO> policyFiles) {
        this.policyFiles = policyFiles;
    }

    public List<FileVO> getAttachedFiles() {
        if (null == offlineOrder || StringUtils.isBlank(offlineOrder.getAttachedFile())) {
            return attachedFiles;
        }
        try {
            List<FileVO> fileVOS = JSON.parseArray(offlineOrder.getAttachedFile(), FileVO.class);
            for (FileVO fileVO : fileVOS) {
                fileVO.setFileUrl(MyStringUtils.createFileUrl("edu-svc", BusinessTypeCodeEnum.F002.getCode(), String.valueOf(offlineOrder.getId()), "0", fileVO.getFileId()));
            }
            return fileVOS;
        } catch (Exception e) {
            log.error("ID为{}的线下投保单，附件Json格式不正确!", offlineOrder.getId());
        }
        return attachedFiles;
    }

    public void setEvidenceFiles(List<FileVO> evidenceFiles) {
        this.evidenceFiles = evidenceFiles;
    }

    public List<FileVO> getEvidenceFiles() {
        if (null == offlineOrder || StringUtils.isBlank(offlineOrder.getEvidenceFile())) {
            return evidenceFiles;
        }
        try {
            List<FileVO> fileVOS = JSON.parseArray(offlineOrder.getEvidenceFile(), FileVO.class);
            for (FileVO fileVO : fileVOS) {
                fileVO.setFileUrl(MyStringUtils.createFileUrl("edu-svc", BusinessTypeCodeEnum.F004.getCode(), String.valueOf(offlineOrder.getId()), "0", fileVO.getFileId()));
            }
            return fileVOS;
        } catch (Exception e) {
            log.error("ID为{}的线下投保单，佐证文件Json格式不正确!", offlineOrder.getId());
        }
        return attachedFiles;
    }

    public void setAttachedFiles(List<FileVO> attachedFiles) {
        this.attachedFiles = attachedFiles;
    }

    public OfflineOrder getOfflineOrder() {
        return offlineOrder;
    }

    public void setOfflineOrder(OfflineOrder offlineOrder) {
        this.offlineOrder = offlineOrder;
    }

    public List<OfflinePolicy> getOfflinePolicies() {
        return offlinePolicies;
    }

    public void setOfflinePolicies(List<OfflinePolicy> offlinePolicies) {
        this.offlinePolicies = offlinePolicies;
    }
}
